package edu.lsnu.equipment_management_system.service.impl;

import edu.lsnu.equipment_management_system.exception.NoPowerException;
import edu.lsnu.equipment_management_system.mapper.select.nopaging.PowerCheckMapper;
import edu.lsnu.equipment_management_system.mapper.select.paging.SearchEquipmentMapperWithPaging;
import edu.lsnu.equipment_management_system.pojo.domain.PageBean;
import edu.lsnu.equipment_management_system.pojo.domain.PageBeanDeal;
import edu.lsnu.equipment_management_system.pojo.domain.UseStatusConstant;
import edu.lsnu.equipment_management_system.pojo.vo.out.information_list.used_using.DeviceVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.information_list.used_using.EquipmentVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.session.UserVo;
import edu.lsnu.equipment_management_system.pojo.vo.sql_conditions.PageBeginPageSizeUserNoAndConditionVo;
import edu.lsnu.equipment_management_system.pojo.vo.sql_conditions.UserNoAndConditionVo;
import edu.lsnu.equipment_management_system.service.CheckUserAndEquipmentDoesNotExistService;
import edu.lsnu.equipment_management_system.service.SearchEquipmentInformationService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author mzywucai
 * @Description
 * @date 2019/1/4
 */
@Service
@Data
@PropertySource({"classpath:resource.properties"})
@ConfigurationProperties(prefix = "page.search")
public class SearchEquipmentInformationServiceImpl implements SearchEquipmentInformationService {
    private Integer size;
    @Autowired
    SearchEquipmentMapperWithPaging searchMapper;
    @Autowired
    PowerCheckMapper powerCheckMapper;
    @Autowired
    UseStatusConstant useStatusConstant;
    @Autowired
    CheckUserAndEquipmentDoesNotExistService checkUserAndEquipmentDoesNotExistService;

    @Override
    public PageBean<DeviceVo> searchEquipment(Integer currentPage, String condition, String userNo) {
        // 获得总count
        UserNoAndConditionVo userNoAndConditionVo = new UserNoAndConditionVo();
        userNoAndConditionVo.setUserNo(userNo);
        userNoAndConditionVo.setCondition(condition);
        int count = searchMapper.searchEquipmentCount(userNoAndConditionVo);

        // 构造pageBean
        PageBean<DeviceVo> pageBeanContain = new PageBean<>();

        PageBean<DeviceVo> pageBean = (PageBean<DeviceVo>) PageBeanDeal.getPageBean(count, currentPage, this.size, pageBeanContain);

        // 查询当前页的List。
        PageBeginPageSizeUserNoAndConditionVo pageBeginPageSizeUserNoAndConditionVo =
                new PageBeginPageSizeUserNoAndConditionVo();

        // 当前页开始的条数：(当前页 - 1) * 页容量
        Integer pageBegin = (pageBean.getCurrentPage() - 1)*pageBean.getPageSize();
        pageBeginPageSizeUserNoAndConditionVo.setCondition(condition);
        pageBeginPageSizeUserNoAndConditionVo.setPageBegin(pageBegin);
        pageBeginPageSizeUserNoAndConditionVo.setPageSize(pageBean.getPageSize());
        pageBeginPageSizeUserNoAndConditionVo.setUserNo(userNo);


        List<DeviceVo> deviceVos =
                searchMapper.searchEquipment(pageBeginPageSizeUserNoAndConditionVo);
        pageBean.setContent(deviceVos);

        return pageBean;
    }

    @Override
    public EquipmentVo userGetEquipmentDetail(String equipmentNo, HttpSession session) throws NoPowerException {
        EquipmentVo equipmentDetail;
        UserVo userVo = (UserVo) session.getAttribute("user");
        String userNo = userVo.getUserNo();
        powerCheck(equipmentNo, userNo);

        equipmentDetail = getEquipmentDetail(equipmentNo);

        return equipmentDetail;
    }

    @Override
    public EquipmentVo adminGetEquipmentDetail(String equipmentNo) {
        EquipmentVo equipmentDetail = null;
        boolean res = checkUserAndEquipmentDoesNotExistService.checkEquipmentNoDoesNotExist(equipmentNo);
        if (!res) {
            equipmentDetail = getEquipmentDetail(equipmentNo);
        }

        return equipmentDetail;
    }



    private void powerCheck(String equipmentNo, String userNo) throws NoPowerException {
        List<String> allEquipmentNo = powerCheckMapper.getALLUsedEquipmentNo(userNo);

        if (! allEquipmentNo.contains(equipmentNo)) {
            throw new NoPowerException();
        }
    }

    private EquipmentVo getEquipmentDetail(String equipmentNo) {
        EquipmentVo equipmentDetail;
        String status = searchMapper.equipmentStatus(equipmentNo);
        if (status.equals(useStatusConstant.getUsing()) ||
                status.equals(useStatusConstant.getRepair()) ||
                status.equals(useStatusConstant.getSpare())) {
            equipmentDetail = searchMapper.getHasOwnerEquipmentDetail(equipmentNo);
        } else {
            equipmentDetail = searchMapper.getOthersEquipmentDetail(equipmentNo);
        }

        return equipmentDetail;
    }

}
